package com.mindskip.gushuang.service.impl;

import com.mindskip.gushuang.service.FileUpload;

import com.mindskip.gushuang.utility.MinioUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.InputStream;

@Service
public class FileUploadImpl implements FileUpload {
    @Autowired
    MinioUtil minioUtil;

    @Value("${minio.bucketName}")
    private String bucketName;

    @Value("${minio.objectName}")
    private String objectName;

    @Value("${minio.url}")
    private String url;
    @Value("${minio.accessKey}")
    private String accessKey;

    @Value("${minio.secretKey}")
    private String secretKey;

    //private final Logger logger = LoggerFactory.getLogger(FileUpload.class);
    //private final SystemConfig systemConfig;
    //
    //@Autowired
    //public FileUploadImpl(SystemConfig systemConfig) {
    //    this.systemConfig = systemConfig;
    //}

    //@Override
    //public String uploadFile(InputStream inputStream, long size, String extName) {
    //    QnConfig qnConfig = systemConfig.getQn();
    //    Configuration cfg = new Configuration(Region.region2());
    //    UploadManager uploadManager = new UploadManager(cfg);
    //    Auth auth = Auth.create(qnConfig.getAccessKey(), qnConfig.getSecretKey());
    //    String upToken = auth.uploadToken(qnConfig.getBucket());
    //    try {
    //        Response response = uploadManager.put(inputStream, null, upToken, null, null);
    //        DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
    //        return qnConfig.getUrl() + "/" + putRet.key;
    //    } catch (QiniuException ex) {
    //        logger.error(ex.getMessage(), ex);
    //    }
    //    return null;
    //}

    @Override
    public String uploadFile(InputStream inputStream, long size, String extName) {

        try {
            minioUtil.createBucket(bucketName);
            if (objectName != null) {
                minioUtil.uploadFile(inputStream, bucketName, objectName + "/" + extName);
                return  url+"/"+bucketName+"/"+objectName+"/"+extName;
            } else {
                minioUtil.uploadFile(inputStream, bucketName, extName);
                return url + "/" + bucketName + "/" + extName;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        }
        return null;
    }
}
